package com.wisemoresystem.seal.action.mac.push;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_bank;
import com.wisemoresystem.seal.model.t_mac;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.BankService;
import com.wisemoresystem.seal.service.MacMoveLogService;
import com.wisemoresystem.seal.service.MacService;
import com.wisemoresystem.seal.util.json.JsonTools;

/**
 * 分发
 * 
 * @author 冯晓东
 *
 */
@Controller("macPush")
@Scope("prototype")
@RequestMapping("/ix/mac/push")
public class MacPushAction extends ProjBaseAction {

	@Autowired
	private MacService macService;
	@Autowired
	private BankService bService;
	@Autowired
	private MacMoveLogService macLogService;

	/**
	 * 印控仪分发列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> macList = macService.selectInStockList(
				srh_name, page);

		ModelAndView mv = new ModelAndView("/jsp/mac/push/list");
		mv.addObject("macList", macList);
		return mv;
	}

	/**
	 * 添加页面
	 * 
	 * @param mac_id
	 *            印控仪id
	 * @return
	 */
	@RequestMapping("/push_form")
	public ModelAndView push_form(String mac_id) {

		t_mac mac = oc.findObjById(mac_id, t_mac.class);
		t_user user = oc.findObjById(mac.getUser_id(), t_user.class);
		t_bank bank = oc.findObjById(mac.getBank_id(), t_bank.class);

		ModelAndView mv = new ModelAndView("/jsp/mac/push/push_form");
		mv.addObject("mac", mac);
		mv.addObject("user", user);
		mv.addObject("bank", bank);
		return mv;
	}

	/**
	 * 树结构信息列表
	 * 
	 * @return
	 */
	@RequestMapping("/get_userbank")
	public ModelAndView get_userbank() {

		List<Map<String, Object>> list = bService.selectUserBankList();
		String bankJson = JsonTools.toJson(list);

		ModelAndView mv = new ModelAndView("/jsp/bank/get/getbank");
		mv.addObject("bankJson", bankJson);
		return mv;
	}

	/**
	 * 分发
	 * 
	 * @param mac_id
	 *            设备id
	 * @param dest_bank_id
	 *            分发机构id
	 * @param note
	 *            分发备注
	 * @param bank_name
	 *            机构名称
	 * @return
	 */
	@RequestMapping("/push")
	public ModelAndView push(String mac_id, String dest_bank_id, String note,
			String bank_name) {

		macLogService.push(mac_id, dest_bank_id, note);
		logT.logH("印控仪分发", t_mac.class, mac_id);

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		this.setTipMsg("分发成功!", Tip.Type.success);
		return mv;
	}

	/**
	 * 批量分发
	 * 
	 * @param check_ids
	 *            分发的印控仪的id
	 * @param bank_id
	 *            机构id
	 * @param note
	 *            分发备注
	 * @return
	 */
	@RequestMapping("/push_result")
	public ModelAndView push_result(String check_ids, String bank_id,
			String note) {

		String[] ids = check_ids.split(",");
		for (String mac_id : ids) {
			if (ChkTools.isNotNull(mac_id)) {
				macLogService.push(mac_id, bank_id, note);
				logT.logH("印控仪分发", t_mac.class, mac_id);
			}
		}

		ModelAndView mv = new ModelAndView("redirect:list.htm");
		this.setTipMsg("分发成功!", Tip.Type.success);
		return mv;
	}

}
